package com.jlin.base;

import java.io.File;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import com.jlin.base.abstracttyoe.AbstractProduct;

/**
 * @Title:TsMain.java
 * @Description:
 * @author lijilin@hitao.com
 * @date 2011-6-29 ÏÂÎç06:34:07
 */

public class TsMain {
	
	public static void main(String[] args) {
//		A b = new C();
//		tsMap();
//		try{
//			error1();
//		}catch(Exception e){
//			System.out.println(e.getMessage());
//		}
//		tsTFor();
//		tsHashMapSort();
//		tsHashMapSort2();
//		removeList();
//		tsRMHashMapSort();
//		tsIntHashMap();
//		getFileName();
//		tsLong();
//		tsSplit();
//		tsSet();
//		tsXhangeArrayList();
//		systemProperties();
//		AbstractProduct.prt("that's ok!");
//		String LINE_SEP = System.getProperty("line.separator");
//		System.out.println("=====");
//		System.out.println(LINE_SEP);
//		System.out.println("=====");
//		replace();
//		beyondCheck();
//		autoPO();
//		tsLinkedList();
//		System.out.println(0<=0.00);
//		System.out.println(7 % -5);
//		System.out.println((7%2));
//		System.out.println(mod(7,-5));
//		copyArray();
//		copyArrays();
//		mm();
//		tsObject();
//		tsRefer();
//		swap();
//		swapT();
//		prtMod();
//		copyArray();
//		plus(9);
//		consumeTime();
		new TsMain().getA();
		new TsMain().fetchA();
	}
	
	private final Object obj = new Object();
	
	public void fetchA() {
		synchronized(obj) {
			fetchB();
			System.out.println("fetch A");
		}
	}
	
	private void fetchB() {
		synchronized(obj) {
			System.out.println("fetch B");
		}
	}
	
	public synchronized void  getA() {
		getB();
		System.out.println("getA()");
	}
	
	private synchronized void  getB() {
		System.out.println("getB()");
	}
	
	static void consumeTime() {
		long startTime = System.currentTimeMillis();
		long j = 0l;
		for (int i = 0; i<10700; i++) {
			j++;
			System.out.println("xxx");
		}
		long endTime = System.currentTimeMillis();
		System.out.println("1w´ÎÏûºÄµÄÊ±¼ä£º"+(endTime-startTime)+"ºÁÃë");
	}
	
	static void plus(int num){
		for(int i = 1;i<=num; i++) {
			for(int j = 1; j<=i;j++){
				System.out.print(j+"*"+i+"="+j*i+"\t");
			}
			System.out.println();
		}
		
	}
	
	static void copyArray(){
		String a = new String("123");
		ArrayList<String> arr = new ArrayList<String>();
		arr.add(a);
//		a = new String("4123");
//		a = "1234";
		System.out.println(arr.get(0));
	}
	
	static void copyArrays() {
		String a = new String("123");
		Persons p = new Persons();
		p.setName(a);
		ArrayList<Persons> arr = new ArrayList<Persons>();
		arr.add(p);
//		p = new Persons();
//		a = new String("4123");
//		a = "4123";
		p.setName(a);
		System.out.println(arr.get(0).getName());
	}
	
	static void prtMod(){
		int  a = 7936;
		System.out.println(a%1024);
		System.out.println((a%1024)/32);
	}
	
	static void swapT(){
		int a = 0, b = 1;
		a = b + (b=a)*0;
		System.out.println("a:"+a+",b:"+b);
	}
	
	static void swap(){
		int a = 0,b=1;
		a = a + b;
		b = a - b;
		a = a - b;
		System.out.println("a:"+a+",b:"+b);
	}
	
	static void tsRefer(){
		String a = "a";
		String b = new String("a");
		String c = "b";
		String d = a + c;
		String f = "a" + "b";
		
		System.out.println("d.hashCode:"+((Object)d).hashCode()+",f.hashCode:"+((Object)f).hashCode());
		System.out.println("d.hashCode:"+System.identityHashCode(d)+",f.hashCode:"+System.identityHashCode(f));
		d = a;
		System.out.println("d.hashCode:"+System.identityHashCode(d));
		System.out.println(d.equals(f));
		System.out.println(a == b);
		System.out.println(d == f);
	}
	
	static void tsObject(){
		Object o = new Object();
		Object o1 = new Object();
		Object o2 = o;
		System.out.println(o == o2);
		System.out.println(o.equals(o2));
		System.out.println(o == o1);
		System.out.println("o1:"+o1.hashCode()+",o:"+o.hashCode());
	}
	
	/**ÔËËã²Ù×÷·ûºÅºÍÁ¬½Ó·ûºÅ+£¬*/
	static void mm(){
//		System.out.println('2' + '5'); 
//		System.out.println("2"+"5"+' ');
		System.out.println('2'+'5'+"");
		System.out.println(""+'2'+'5');
		System.out.println(1+'2'+'5');
		System.out.println(1l+'2'+'5');
	}
	
	static void tsFinal(){
		final int a = 10;
//		a = a + 1;
	}
	
	static class Persons{
		String name;

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}
		
	}
	static int[][] contirbuteArray(){
		int[][] numbers = new int[][]{{1,2},{2,3},{3,4},{110,223},{-1,5665},{92,55},{234,4212},{123,-19192},
				{230,87438},{13090,-49583},{1,2},{2,3}};
		return numbers;
	}

	static int findMaximum(int[][] numbers) {
		int maxElem = 0;
		for (int i = 0; i < numbers.length; i++) {
			for (int j = 0; j < numbers[i].length; j++) {
				if (numbers[i][j] > maxElem)
					maxElem = numbers[i][j];
			}
		}
		return maxElem;
	}
	public static int mod(int a, int b){
		System.out.println(a = a - (a/b)*b);
		return a;
	}
	
	
	final static Map<String, String> finalMap;
	static {
		finalMap = null;
//		finalMap = new HashMap<String, String>();
	}
	private static void changeFinalInit(){
		
//		finalMap = new HashMap<String, String>();
		
		
	}
	
	private static void tsLinkedList(){
		List<String> linkList = new LinkedList<String>();
		linkList.add(null);
		linkList.add(null);
		linkList.add("1");
		for(String param : linkList){
//			System.out.println(param);
		}
		linkList.remove(null);
		for(String param : linkList){
			System.out.println(param);
		}
	}
	
	private static void autoPO(){
		Integer a = 1;
		Integer b = 2;
		Integer c = 3;
		Integer d = 4;
		Integer e = 321;
		Integer f = 321;
		Long g = 3L;
		System.out.println(c == d);//false
		System.out.println(e == f);//false
		System.out.println(c == (a+b));//true
		System.out.println(c.equals(a + b));//true
		System.out.println(g == (a + b));//true
		System.out.println(g.equals(a+b));//false
		//equal do type check first,then compare value
		//for operation , then turn to primitive, final do value equal
	}
	
	
	private static void beyondCheck(){
		String number = "2152564925";
		if(Long.parseLong(number)>3)
			System.out.println(Long.MAX_VALUE);
//		System.out.println(Integer.parseInt(number));
	}
	
	public static void replace(){
		String aa = "http://item.taobao.com";
		System.out.println(aa.replace("taobao", "amazon"));
	}
	
	public static void systemProperties(){
		Properties p = System.getProperties();
		Enumeration<?> e = p.propertyNames();
		while(e.hasMoreElements()){
			System.out.println(e.nextElement()+":"+p.getProperty((String)e.nextElement()));
		}
	}
	
	public static void tsXhangeArrayList(){
		List<String> list = new ArrayList<String>();
		list.add("1");
		list.add("2");
		list.add("3");
		list.add("4");
		List<String> list2 = new ArrayList<String>();
		for(int i=0; i<list.size(); i++){
			list2.add(i, "prefix"+list.get(i));
		}
		for(String i : list2){
			System.out.println(i);
		}
	}
	
	public static void tsSplit(){
		String a = "hitao|buyNow";
//		a.split(regex)
		for(String b : a.split("\\|")){
			System.out.println(b);
		}
//		System.out.println(a.split("|"));
	}
	
	
	public static void tsLong(){
		Long l = Long.valueOf(1);
		long k = 1;
		if(l.longValue() == k)
			System.out.println(true);
		else 
			System.out.println(false);
		
	}
	
	public static void tsIntHashMap(){
		Map<Integer, Integer> map = new LinkedHashMap<Integer, Integer>();
		map.put(1, 1);
		map.put(2, 2);
		System.out.println(map.get(3));
		
	}
	
	public static void getFileName(){
		String path="";//update
		File f = new File(path);
		for(File ff:f.listFiles()){
			System.out.println(ff.getName());
		}
	}
	
	public static void tsRMHashMapSort(){
		Map<Integer, String> map = new LinkedHashMap<Integer, String>();
		
		map.put(1, "a");
		map.put(3, "c");
		map.put(2, "b");
		int i = 0;
		
		for(Object it : map.keySet().toArray()){
			System.out.print(it);
			System.out.print("---");
			System.out.println(map.get(it));
			i++;
			if(i == 1)
				map.put(4,"d");
//			map.remove(it);
		}
	}
	
	public static void removeList(){
		List<Person> list  = new ArrayList<Person>();
		Person person1 = new Person("lo1",1);
		Person person2 = new Person("lo2",2);
		Person person3 = new Person("lo3",3);
		list.add(person1);
		list.add(person3);
		list.add(person2);
//		for(Person person : list){
//			list.remove(person);
//		}
//		for(int i=0; i<list.size(); i++){
//			System.out.println(list.get(i).getName());
//			list.remove(i);
//			i--;
//			list.add(person1);
//			i++;
//		}
		Person person;
		int i = 0;
		for(Iterator<Person> ite=list.iterator();ite.hasNext();){
			i++;
			person = ite.next();
			System.out.println(person.getName());
			if(person.getList().size() == 0){
				person.setName("kfkd");
				ite.remove();
			}
			if(i == 1){
				list.add(person1);
			}
		}
	}
	
	public static void tsHashMapSort2(){
		Map<Person, String> map = new LinkedHashMap<Person, String>();
		Person person1 = new Person("lo1",1);
		Person person2 = new Person("lo2",2);
		Person person3 = new Person("lo3",3);
		List<String> list  = new ArrayList<String>();
		list.add("1");
		map.put(person1, "1");
		map.put(person3, "3");
		map.put(person2, "2");
		for(Map.Entry<Person, String> object : map.entrySet()){
			System.out.println(object.getKey().getName());
		}
		
	}
	
	public static void tsHashMapSort(){
		Map<Integer, String> map = new LinkedHashMap<Integer, String>();
		
		map.put(1, "a");
		map.put(3, "c");
		map.put(2, "b");
		
		Map<String, Integer> mapMap = new HashMap<String, Integer>();
		mapMap.put("a", 1);
		mapMap.put("c", 3);
		mapMap.put("b", 2);
		for(Integer i: map.keySet()){
			System.out.println(map.get(i));
		}
		System.out.println("==================");
		for(Map.Entry<Integer, String> map1 : map.entrySet()){
			System.out.println(map1.getKey());
		}
		System.out.println("==================");
		List<Map.Entry<String, Integer>> list =  new ArrayList<Map.Entry<String, Integer>>(mapMap.entrySet());
		for(Map.Entry<String, Integer> map2 : list){
			System.out.println(map2.getKey());
		}
	}
	
	public static void tsTFor(){
		for(int i=0;i<10;i++){
			System.out.println("i:"+i);
			for(int j=0;j<10;j++){
				if(j > 2)
					break;
//				System.out.println("j:"+j);
			}
		}
	}
	
	public static void tsMap() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("key", "value");
		for (String key : map.keySet()) {
			System.out.println(map.get(key));
		}
	}
	
	public static void error1(){
		System.out.println("error2");
//		try{
			error2();
//		}catch(Exception e){
//			System.out.println(e.getMessage());
//		}
			System.out.println("error2 end");
	}
	
	
	public static void error2() {
		System.out.println("error1");
			throw new RuntimeException("error1");
	}
	
	public static void tsSet(){
		Set<Long> set = new HashSet<Long>();
		set.add(1L);
		set.add(1L);
		set.add(2L);
		set.add(2L);
		set.add(3L);
		set.add(3L);
		for(Iterator<Long> ite = set.iterator();ite.hasNext();){
			System.out.println(ite.next());
		}
	}
	
}
